// https://leetcode.cn/problems/max-consecutive-ones-iii/submissions/558937709/
class Solution {
public: 
    int longestOnes(vector<int>& v, int k) {
        int l = 0, r = 0;
        int n = v.size();

        int cnt = 0;
        int ans = 0;
        while (r < n) {
            if (!v[r])
                cnt++;
            while (cnt > k) {
                if (!v[l])
                    cnt--;
                l++;
            }
            ans = max(ans, r-l+1);
            r++;
        }
        return ans;
    }
};